package com.nth.rooster.circles.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nth.rooster.circles.dao.CircleDAOI;
import com.nth.rooster.core.entities.RamCircles;
import com.nth.rooster.core.entities.RamUsers;
import com.nth.rooster.core.helper.Mapper;
import com.nth.rooster.core.pojo.CircleBeans;
import com.nth.rooster.core.pojo.User;

@Component
public class CircleService implements CircleServiceI {

	static Logger LOGGER = Logger.getLogger(CircleService.class);

	@Autowired
	private CircleDAOI circleDAOI;

	public Long createCircle(CircleBeans circleBeans, Long userID) {
		LOGGER.debug("Entered::Circle::CircleService::createCircle()");
		RamCircles circles = Mapper.mapCircleFromPojo(circleBeans,null,userID);
		Long circleID = circleDAOI.saveAndUpdateCircle(circles);
		LOGGER.debug("Exited::Circle::CircleService::createCircle()");
		return circleID;
	}
	
	public Long updateCircle(CircleBeans circleBeans, Long userID) {
		LOGGER.debug("Entered::Circle::CircleService::createCircle()");
		RamCircles circle = circleDAOI.getCircleByCircleID(circleBeans.getCircleID());
		circle = Mapper.mapCircleFromPojo(circleBeans,circle,userID);
 		Long circleID = circleDAOI.saveAndUpdateCircle(circle);
		LOGGER.debug("Exited::Circle::CircleService::createCircle()");
		return circleID;
	}

	public RamCircles getCircleByCircleID(Long id) {
		LOGGER.debug("Entered::Circle::CircleService::getCircleByCircleID() ");
		RamCircles circle = circleDAOI.getCircleByCircleID(id);
		LOGGER.debug("Exited::Circle::CircleService::getCircleByCircleID() ");

		return circle;
	}

	public List<CircleBeans> getAllCircles(Boolean withDescription) {
		LOGGER.debug("Entered::Circle::CircleService::getCircleByCircleID() ");
		List<CircleBeans> circles = new ArrayList<CircleBeans>();
		List<RamCircles> ramCircles = circleDAOI.getAllCircles();
		for (RamCircles ramCircle : ramCircles) {
			CircleBeans circleBeans = new CircleBeans();
			circleBeans.setCircleID(ramCircle.getCircleID());
			circleBeans.setCircleTitle(ramCircle.getCircleName());
			if (withDescription) {
				circleBeans.setCircleDescrition(ramCircle
						.getCircleDescription());
			}
			circles.add(circleBeans);
		}
		LOGGER.debug("Exited::Circle::CircleService::getCircleByCircleID() ");

		return circles;
	}

	public void assignOwnerToCircle(Long id, List<User> users) {
		LOGGER.debug("Entered::Circle::CircleService::assignOwnerToCircle() ");
		RamCircles circle = circleDAOI.getCircleByCircleID(id);

		Set<RamUsers> listOfRamUsers = null;
		if (users != null) {
			listOfRamUsers = new HashSet<RamUsers>();
			for (User user : users) {
				RamUsers ramUsers = new RamUsers();
				ramUsers.setUserId(user.getUserID());
				listOfRamUsers.add(ramUsers);
			}
		}
		//circle.setRamUsers(listOfRamUsers);
		circleDAOI.saveAndUpdateCircle(circle);

		LOGGER.debug("Exited::Circle::CircleService::assignOwnerToCircle() ");

	}

	public CircleDAOI getCircleDAOI() {
		return circleDAOI;
	}

	public void setCircleDAOI(CircleDAOI circleDAOI) {
		this.circleDAOI = circleDAOI;
	}

}
